package com.b.sts.controller;

import com.b.sts.entity.ProductEntity;
import com.b.sts.enums.ProductType;
import com.b.sts.service.ProductService;
import com.b.sts.util.JwtUtil;
import com.b.sts.util.ResultUtil;
import com.b.sts.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author b
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    ProductService service;

    @GetMapping("/{id}")
    public ResultVO<ProductEntity> getById(@PathVariable Integer id) {
        ProductEntity data = service.get(id);
        return ResultUtil.success(data);
    }

    @GetMapping("/list")
    public ResultVO<List<ProductEntity>> list(ProductEntity entity) {
        List<ProductEntity> data = service.list(entity);
        return ResultUtil.success(data);
    }

    @GetMapping("/list/type")
    public ResultVO<List<String>> typeList() {
        List<String> list = ProductType.types;
        return ResultUtil.success(list);
    }

    @GetMapping("/e/my")
    public ResultVO<List<ProductEntity>> my(ProductEntity entity) {
        entity.setSellerId(JwtUtil.getUid());
        List<ProductEntity> data = service.my(entity);
        return ResultUtil.success(data);
    }

    @PostMapping
    public ResultVO<Boolean> add(@RequestBody ProductEntity entity) {
        entity.setSellerId(JwtUtil.getUid());
        boolean isSuccess = service.add(entity);
        return ResultUtil.autoDo(isSuccess);
    }

    @DeleteMapping("/{id}")
    public ResultVO<Boolean> del(@PathVariable Integer id) {
        boolean isSuccess = service.del(id);
        return ResultUtil.autoDo(isSuccess);
    }

    @PutMapping
    public ResultVO<Boolean> update(@RequestBody ProductEntity entity) {
        boolean isSuccess = service.update(entity);
        return ResultUtil.autoDo(isSuccess);
    }

    @PostMapping("/confirmSales/{buyer}")
    public ResultVO<Boolean> confirmSales(@RequestBody ProductEntity entity, @PathVariable String buyer) {
        boolean isSuccess = service.confirmSales(entity,buyer);
        return ResultUtil.autoDo(isSuccess);
    }
}
